//
//  NSString+Helper.m
//  02.用户登录&注册
//
//  Created by 刘凡 on 13-11-28.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "NSString+Helper.h"

@implementation NSString (Helper)

#pragma mark 清空字符串中的空白字符
- (NSString *)trimString
{
    return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

#pragma mark 是否空字符串
- (BOOL)isEmptyString
{
    return (self.length == 0);
}

#pragma mark 返回沙盒中的文件路径
- (NSString *)documentsPath
{
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    return [path stringByAppendingPathComponent:self];
}

#pragma mark 写入系统偏好
- (void)saveToNSDefaultsWithKey:(NSString *)key
{
    [[NSUserDefaults standardUserDefaults] setObject:self forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];

}

//+(NSString *)stringFromNSdefaultWithKey:(NSString *)key;

#pragma mark -是否包含字符串ios8之前版本使用
- (BOOL)ContainsStringIOS7:(NSString*)other {
    NSRange range = [self rangeOfString:other];
    return range.length != 0;
}

// 过滤所有表情  https://gist.github.com/cihancimen/4146056
- (BOOL)containsEmoji {
    __block BOOL returnValue = NO;
    [self enumerateSubstringsInRange:NSMakeRange(0, [self length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:
     ^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
         
         const unichar hs = [substring characterAtIndex:0];
         // surrogate pair
         if (0xd800 <= hs && hs <= 0xdbff) {
             if (substring.length > 1) {
                 const unichar ls = [substring characterAtIndex:1];
                 const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                 if (0x1d000 <= uc && uc <= 0x1f77f) {
                     returnValue = YES;
                 }
             }
         } else if (substring.length > 1) {
             const unichar ls = [substring characterAtIndex:1];
             if (ls == 0x20e3) {
                 returnValue = YES;
             }
             
         } else {
             // non surrogate
             if (0x2100 <= hs && hs <= 0x27ff) {
                 returnValue = YES;
             } else if (0x2B05 <= hs && hs <= 0x2b07) {
                 returnValue = YES;
             } else if (0x2934 <= hs && hs <= 0x2935) {
                 returnValue = YES;
             } else if (0x3297 <= hs && hs <= 0x3299) {
                 returnValue = YES;
             } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
                 returnValue = YES;
             }
         }
     }];
    
    return returnValue;
}


-(NSString *)zhongWenZhuanMa
{
    return [self stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}

-(NSAttributedString *)qianWeiKongGeStringWithColor:(UIColor *)clor{
    if (!self) {
        return nil;
    }
    NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc] initWithString:@" " attributes:@{NSForegroundColorAttributeName:kMainColorText, NSFontAttributeName:kMainFont(17)}];
    NSMutableAttributedString *str2 =  [[NSMutableAttributedString alloc] initWithString:self attributes:@{NSForegroundColorAttributeName:clor, NSFontAttributeName:kMainFont(17)}];
    
    
    NSMutableAttributedString *str3 =  [[NSMutableAttributedString alloc] initWithString:@"X" attributes:@{NSForegroundColorAttributeName:[UIColor clearColor], NSFontAttributeName:kMainFont(17)}];
    
    NSMutableAttributedString *strTotal = [NSMutableAttributedString new];
    [strTotal appendAttributedString:str1];
    [strTotal appendAttributedString:str2];
    [strTotal appendAttributedString:str3];
    return strTotal;
}
-(NSAttributedString *)qianWeiKongGeString
{
    if (!self) {
        return nil;
    }
    NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc] initWithString:@" " attributes:@{NSForegroundColorAttributeName:kMainColorText, NSFontAttributeName:kMainFont(17)}];
    NSMutableAttributedString *str2 =  [[NSMutableAttributedString alloc] initWithString:self attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:kMainFont(17)}];
    
    
    NSMutableAttributedString *str3 =  [[NSMutableAttributedString alloc] initWithString:@"X" attributes:@{NSForegroundColorAttributeName:[UIColor clearColor], NSFontAttributeName:kMainFont(17)}];
    
    NSMutableAttributedString *strTotal = [NSMutableAttributedString new];
    [strTotal appendAttributedString:str1];
    [strTotal appendAttributedString:str2];
    [strTotal appendAttributedString:str3];
    return strTotal;
}
-(NSAttributedString *)qianWeiKongGeString:(CGFloat)size
{
    
    if (!self) {
        return nil;
    }
    NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc] initWithString:@" " attributes:@{NSForegroundColorAttributeName:kMainColorText, NSFontAttributeName:kMainFont(size)}];
    NSMutableAttributedString *str2 =  [[NSMutableAttributedString alloc] initWithString:self attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:kMainFont(size)}];
    
    
    NSMutableAttributedString *str3 =  [[NSMutableAttributedString alloc] initWithString:@"X" attributes:@{NSForegroundColorAttributeName:[UIColor clearColor], NSFontAttributeName:kMainFont(size)}];
    
    NSMutableAttributedString *strTotal = [NSMutableAttributedString new];
    [strTotal appendAttributedString:str1];
    [strTotal appendAttributedString:str2];
    [strTotal appendAttributedString:str3];
    return strTotal;

}


+(NSString*)TimeformatFromSeconds:(NSInteger)seconds
{
    //format of hour
    NSString *str_hour = [NSString stringWithFormat:@"%02d",seconds/3600];
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%02d",(seconds%3600)/60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%02d",seconds%60];
    //format of time
    NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
    return format_time;
}
@end
